package cliente;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Cliente extends JFrame {

	private JPanel contentPane;
	private JButton btnPP;
	private JList list;
	private String[] archivos;
	private Reproductor rp;
	private ClienteTCPUDP client;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Cliente frame = new Cliente();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Cliente() {
		setTitle("Lista de videos");
		client=new ClienteTCPUDP();
		archivos=client.obtenerLista();
		
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 380, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		list = new JList(archivos);
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				descargarVideo();
			}
		});
		JScrollPane listScroller = new JScrollPane(list);		
		listScroller.setBounds(10, 10, 350, 400);
		listScroller.setPreferredSize(new Dimension(171, 422));
		contentPane.add(listScroller);
		
		btnPP = new JButton("Pause");
		btnPP.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				pausa();
			}
		});
		btnPP.setBounds(10, 439, 89, 23);
		contentPane.add(btnPP);
		
		JButton btnCerrarVideo = new JButton("Cerrar Video");
		btnCerrarVideo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cerrarVideo();
			}
		});
		btnCerrarVideo.setBounds(109, 439, 114, 23);
		contentPane.add(btnCerrarVideo);
		
		JButton btnCerrarAplicacion = new JButton("Cerrar aplicacion");
		btnCerrarAplicacion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cerrarApp();
			}
		});
		btnCerrarAplicacion.setBounds(246, 439, 114, 23);
		contentPane.add(btnCerrarAplicacion);
	}

	protected void cerrarApp() {
		cerrarVideo();
		this.dispose();		
	}

	protected void cerrarVideo() {
		rp.cerrar();
		client.pararDescarga();
	}

	protected void pausa() {
		if(rp.playAndPause()){
			btnPP.setText("Pause");
		}else{
			btnPP.setText("Play");
		}
		
	}

	protected void descargarVideo() {
		if(list.getSelectedIndex()>=0){
			String fichero=client.indicarArchivo(archivos[list.getSelectedIndex()]);
			rp=new Reproductor(fichero);
			Thread descarga=new Thread(client);
			Thread reproducir=new Thread(rp);
			descarga.start();
			reproducir.start();			
		}
		
	}
}
